using System;
using System.Collections;
using UnityEngine;

public class GUICraftListLoader : MonoBehaviour
{
	public RectTransform Canvas;

	public RectTransform GUICraftPrefab;

	public RectTransform GUICraftingPrefab;

	public RectTransform GUICraftNeed;

	private int indexSelected = -1;

	private void Start()
	{
		SetupCrafterList();
	}

	private void OnEnable()
	{
		SetupCrafterList();
	}

	private void SetupCrafterList()
	{
		if (UnitZ.itemCraftManager == null || Canvas == null || GUICraftPrefab == null)
		{
			return;
		}
		Clear();
		float num = 0f;
		for (int i = 0; i < UnitZ.itemCraftManager.ItemCraftList.Length; i++)
		{
			GameObject gameObject = GUICraftPrefab.gameObject;
			if (i == indexSelected)
			{
				gameObject = GUICraftingPrefab.gameObject;
			}
			GameObject gameObject2 = UnityEngine.Object.Instantiate(gameObject, Vector3.zero, Quaternion.identity);
			RectTransform component = gameObject2.GetComponent<RectTransform>();
			GUICraft component2 = gameObject2.GetComponent<GUICraft>();
			if ((bool)component2)
			{
				component2.Crafter = UnitZ.itemCraftManager.ItemCraftList[i];
				component2.CrafterLoader = this;
				component2.Index = i;
			}
			float num2 = 0f;
			if (i == indexSelected)
			{
				ItemCrafter crafter = UnitZ.itemCraftManager.ItemCraftList[i];
				Vector2 sizeDelta = component.sizeDelta;
				num2 = DrawNeeds(crafter, sizeDelta.y + num);
			}
			gameObject2.transform.SetParent(Canvas.gameObject.transform);
			component.anchoredPosition = new Vector2(5f, 0f - num);
			component.localScale = gameObject.transform.localScale;
			float num3 = num;
			Vector2 sizeDelta2 = component.sizeDelta;
			num = num3 + (sizeDelta2.y + num2);
		}
		RectTransform canvas = Canvas;
		Vector2 sizeDelta3 = Canvas.sizeDelta;
		canvas.sizeDelta = new Vector2(sizeDelta3.x, num);
	}

	private float DrawNeeds(ItemCrafter crafter, float offset)
	{
		float num = 0f;
		for (int i = 0; i < crafter.ItemNeeds.Length; i++)
		{
			GameObject gameObject = UnityEngine.Object.Instantiate(GUICraftNeed.gameObject, Vector3.zero, Quaternion.identity);
			gameObject.transform.SetParent(Canvas.gameObject.transform);
			GUIItemDataNeed component = gameObject.GetComponent<GUIItemDataNeed>();
			if ((bool)component)
			{
				component.Item = crafter.ItemNeeds[i].Item;
				component.Need = crafter.ItemNeeds[i].Num;
				if (UnitZ.playerManager != null && UnitZ.playerManager.PlayingCharacter != null)
				{
					component.Inventory = UnitZ.playerManager.PlayingCharacter.inventory;
				}
			}
			RectTransform component2 = gameObject.GetComponent<RectTransform>();
			RectTransform rectTransform = component2;
			Vector2 sizeDelta = component2.sizeDelta;
			rectTransform.anchoredPosition = new Vector2(5f, 0f - (sizeDelta.y * (float)i + offset));
			component2.localScale = GUICraftNeed.gameObject.transform.localScale;
			float num2 = num;
			Vector2 sizeDelta2 = component2.sizeDelta;
			num = num2 + sizeDelta2.y;
		}
		return num;
	}

	private void Clear()
	{
		if (!(Canvas == null))
		{
			IEnumerator enumerator = Canvas.transform.GetEnumerator();
			try
			{
				while (enumerator.MoveNext())
				{
					Transform transform = (Transform)enumerator.Current;
					UnityEngine.Object.Destroy(transform.gameObject);
				}
			}
			finally
			{
				IDisposable disposable;
				if ((disposable = (enumerator as IDisposable)) != null)
				{
					disposable.Dispose();
				}
			}
		}
	}

	public void SelectCraft(int index, ItemCrafter crafter)
	{
		if (indexSelected == index)
		{
			indexSelected = -1;
		}
		else
		{
			indexSelected = index;
		}
		SetupCrafterList();
	}

	private void Update()
	{
	}
}
